
<template>
  <div class="app-container">

    <el-card v-for="group in itemlist" :key="group.group">
      <div slot="header" class="clearfix">
        <span>{{ group.name }}</span>
      </div>

      <el-table
        :data="group.items"
        style="width: 100%;margin-bottom: 20px;"
        row-key="path"
        border
        default-expand-all
      >
        <el-table-column prop="id" label="ID" />
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="item" label="字段" />
        <el-table-column prop="value" label="属性" />

        <el-table-column prop="encrypt" label="加密">
          <template slot-scope="scope">
            <span v-if="scope.row.encrypt === 1">已加密</span>
            <span v-else>未加密</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="150">
          <template #default="scope">
            <el-button size="small" @click="editRowVisible(group.group, scope.row)">编辑</el-button>
            <!-- <el-button type="danger" size="small" @click="delRowHandle(scope.row)">删除</el-button> -->
          </template>
        </el-table-column>
      </el-table>

    </el-card>
    <el-dialog :visible.sync="dialogFormVisible" title="">
      <el-form ref="form" :model="form">
        <el-form-item
          label="名称"
          :label-width="formLabelWidth"
          prop="name"
          :rules="[
            { required: true, message: '描述不能为空'}
          ]"
        >
          <el-input v-model="form.name" autocomplete="off" disabled />
        </el-form-item>
        <el-form-item
          label="字段"
          :label-width="formLabelWidth"
          prop="item"
          :rules="[
            { required: true, message: '字段不能为空'}
          ]"
        >
          <el-input v-model="form.item" autocomplete="off" disabled />
        </el-form-item>

        <el-form-item
          label="属性"
          :label-width="formLabelWidth"
          prop="value"
          :rules="[
            { required: true, message: '属性不能为空'}
          ]"
        >
          <el-input v-model="form.value" autocomplete="off" />
        </el-form-item>
        <el-form-item
          label="加密"
          :label-width="formLabelWidth"
          prop="encrypt"
        >
          <el-switch
            v-model="form.encrypt"
            :active-value="1"
            :inactive-value="0"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="addRowHandle('form')">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { getItems, addRow, delRow, editRow } from '@/api/system/config'

export default {
  data() {
    return {
      itemlist: [],
      dialogFormVisible: false,
      formLabelWidth: '120px',
      form: {}
    }
  },
  created() {
    this.fetchData()
  },
  methods: {
    fetchData() {
      getItems().then(response => {
        this.itemlist = response.data
      })
    },
    editRowVisible(group, row) {
      this.form = {
        group: group,
        item: row.item,
        value: row.encrypt ? '' : row.value,
        id: row.id,
        encrypt: row.encrypt,
        name: row.name
      }
      this.dialogFormVisible = true
    },
    delRowHandle(row) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // 执行调用删除接口
          delRow({ id: row.id }).then(response => {
            this.$message({
              type: 'success',
              message: '删除成功!'
            })
            this.fetchData()
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    addRowHandle(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.id !== undefined) {
            editRow(this.form.id, this.form).then(response => {
              this.$message({
                type: 'success',
                message: '编辑成功'
              })
              this.fetchData()
            })
          } else {
            addRow(this.form).then(response => {
              this.$message({
                type: 'success',
                message: '添加成功'
              })
              this.fetchData()
            })
          }
          this.dialogFormVisible = false
          this.fetchData()
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

    <style>

</style>
